package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.LbsPointRpcService;
import com.hunttown.mes.rpc.domain.LbsPointDTO;
import com.hunttown.mes.rpc.domain.query.LbsPointDTOQuery;
import com.hunttown.mes.service.LbsPointService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.LbsPointConverts.convert;

/**
 * created by wangjunfu on 2021-07-05 11:06:17
 */
@Service
public class LbsPointRpcServiceImpl implements LbsPointRpcService {

    private final Logger logger = LoggerFactory.getLogger(LbsPointRpcServiceImpl.class);

    private final LbsPointService lbsPointService;

    @Autowired
    public LbsPointRpcServiceImpl(LbsPointService lbsPointService) {
        this.lbsPointService = lbsPointService;
    }

    @Override
    public LbsPointDTO insert(LbsPointDTO lbsPointDTO) {
        try {
            return convert(lbsPointService.insert(convert(lbsPointDTO)));
        } catch (Exception e) {
            logger.error("LbsPoint create failed.", e);
            throw new RuntimeException("LbsPoint create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(LbsPointDTO lbsPointDTO) {
        try {
            return lbsPointService.updateInfoById(convert(lbsPointDTO));
        } catch (Exception e) {
            logger.error("LbsPoint update by id failed.", e);
            throw new RuntimeException("LbsPoint update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(LbsPointDTOQuery lbsPointDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (lbsPointDTOQuery.getW_id() != null && lbsPointDTOQuery.getW_id() > 0) {
                query.put("w_id", lbsPointDTOQuery.getW_id());
            }

            if (lbsPointDTOQuery.getIds() != null && !lbsPointDTOQuery.getIds().equals("")) {
                query.put("ids", lbsPointDTOQuery.getIds());
            }

            if (lbsPointDTOQuery.getEntId() != null) {
                query.put("w_entId", lbsPointDTOQuery.getEntId());
            }

            if (lbsPointDTOQuery.getIsUpload() != null) {
                query.put("isUpload", lbsPointDTOQuery.getIsUpload());
            }

            if (lbsPointDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", lbsPointDTOQuery.getDeleteFlag());
            }

            return lbsPointService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("LbsPoint update by query failed.", e);
            throw new RuntimeException("LbsPoint update by query failed, Please order your code.", e);
        }
    }

    @Override
    public LbsPointDTO selectById(Integer id) {
        try {
            return convert(lbsPointService.selectById(id));
        } catch (Exception e) {
            logger.error("LbsPoint select by id failed.", e);
            throw new RuntimeException("LbsPoint select by id failed, Please order your code.", e);
        }
    }

    @Override
    public LbsPointDTO selectObjByQuery(LbsPointDTOQuery lbsPointDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (lbsPointDTOQuery.getUserId() != null) {
                query.put("userId", lbsPointDTOQuery.getUserId());
            }

            if (lbsPointDTOQuery.getTrackId() != null) {
                query.put("trackId", lbsPointDTOQuery.getTrackId());
            }

            if (StringUtils.isNotBlank(lbsPointDTOQuery.getShowDate())) {
                query.put("showDate", lbsPointDTOQuery.getShowDate());
            }

            if (StringUtils.isNotBlank(lbsPointDTOQuery.getStartDate())) {
                query.put("startDate", lbsPointDTOQuery.getStartDate());
            }

            if (StringUtils.isNotBlank(lbsPointDTOQuery.getEndDate())) {
                query.put("endDate", lbsPointDTOQuery.getEndDate());
            }

            if (lbsPointDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", lbsPointDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(lbsPointDTOQuery.getOrderBy())) {
                query.put("orderBy", lbsPointDTOQuery.getOrderBy());
            }

            return convert(lbsPointService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("LbsPoint select obj by query failed.", e);
            throw new RuntimeException("LbsPoint select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public LbsPointDTO selectObjWithoutEnt(LbsPointDTOQuery lbsPointDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (lbsPointDTOQuery.getUserId() != null) {
                query.put("userId", lbsPointDTOQuery.getUserId());
            }

            if (lbsPointDTOQuery.getTrackId() != null) {
                query.put("trackId", lbsPointDTOQuery.getTrackId());
            }

            if (StringUtils.isNotBlank(lbsPointDTOQuery.getShowDate())) {
                query.put("showDate", lbsPointDTOQuery.getShowDate());
            }

            if (StringUtils.isNotBlank(lbsPointDTOQuery.getStartDate())) {
                query.put("startDate", lbsPointDTOQuery.getStartDate());
            }

            if (StringUtils.isNotBlank(lbsPointDTOQuery.getEndDate())) {
                query.put("endDate", lbsPointDTOQuery.getEndDate());
            }

            if (lbsPointDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", lbsPointDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(lbsPointDTOQuery.getOrderBy())) {
                query.put("orderBy", lbsPointDTOQuery.getOrderBy());
            }

            return convert(lbsPointService.selectObjWithoutEnt(query));
        } catch (Exception e) {
            logger.error("LbsPoint select obj without ent failed.", e);
            throw new RuntimeException("LbsPoint select obj without ent failed, Please order your code.", e);
        }
    }

    @Override
    public Page<LbsPointDTO> getListForPage(LbsPointDTOQuery lbsPointDTOQuery) {
        Query query = Query.page(lbsPointDTOQuery.getPn(), lbsPointDTOQuery.getPageSize());

        if (lbsPointDTOQuery.getId() != null && lbsPointDTOQuery.getId() > 0) {
            query.put("id", lbsPointDTOQuery.getId());
        }

        if (lbsPointDTOQuery.getIds() != null && !lbsPointDTOQuery.getIds().equals("")) {
            query.put("ids", lbsPointDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(lbsPointDTOQuery.getLikeName())) {
            query.put("likeName", lbsPointDTOQuery.getLikeName());
        }

        if (lbsPointDTOQuery.getEntId() != null) {
            query.put("entId", lbsPointDTOQuery.getEntId());
        }

        if (lbsPointDTOQuery.getTerminalId() != null) {
            query.put("terminalId", lbsPointDTOQuery.getTerminalId());
        }

        if (lbsPointDTOQuery.getTrackId() != null) {
            query.put("trackId", lbsPointDTOQuery.getTrackId());
        }

        if (lbsPointDTOQuery.getIsUpload() != null) {
            query.put("isUpload", lbsPointDTOQuery.getIsUpload());
        }

        if (lbsPointDTOQuery.getStartUnixTime() != null) {
            query.put("startUnixTime", lbsPointDTOQuery.getStartUnixTime());
        }

        if (lbsPointDTOQuery.getEndUnixTime() != null) {
            query.put("endUnixTime", lbsPointDTOQuery.getEndUnixTime());
        }

        if (StringUtils.isNotBlank(lbsPointDTOQuery.getShowDate())) {
            query.put("showDate", lbsPointDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(lbsPointDTOQuery.getStartDate())) {
            query.put("startDate", lbsPointDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(lbsPointDTOQuery.getEndDate())) {
            query.put("endDate", lbsPointDTOQuery.getEndDate());
        }

        if (lbsPointDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", lbsPointDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(lbsPointDTOQuery.getOrderBy())) {
            query.put("orderBy", lbsPointDTOQuery.getOrderBy());
        }

        List<LbsPointDTO> list = convert(lbsPointService.selectByQuery(query));
        Page<LbsPointDTO> page = Page.from(lbsPointDTOQuery.getPn(), lbsPointDTOQuery.getPageSize(), list);
        page.setTotalItems(lbsPointService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return lbsPointService.deleteById(id);
        } catch (Exception e) {
            logger.error("LbsPoint delete failed.", e);
            throw new RuntimeException("LbsPoint delete failed, Please order your code.", e);
        }
    }
}